package com.hzhjhh.view.manageView.navigations;

import java.util.Arrays;
import java.util.List;
import java.util.Vector;

import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTree;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.ibatis.session.SqlSession;

import com.hzhjhh.dto.StockDTO;
import com.hzhjhh.entity.Stock;
import com.hzhjhh.enums.CategoryEnums;
import com.hzhjhh.init.dialog.Mytable;
import com.hzhjhh.mapper.StockMapper;
import com.hzhjhh.util.DBUtils;
import com.hzhjhh.view.manageView.MainFrame;
import com.hzhjhh.view.manageView.menus.accountMng.AccountQueryDialog;
import com.hzhjhh.view.manageView.menus.accountMng.Adminstrator;
import com.hzhjhh.view.manageView.menus.query.QueryMerchandiseInfo;
import com.hzhjhh.view.manageView.menus.query.SaleStatistics;
import com.hzhjhh.view.manageView.menus.query.enums.QueryGoodsShowEnum;
import com.hzhjhh.view.manageView.menus.storage.AddGoodsDialog;
import com.hzhjhh.view.manageView.menus.storage.InOutTableDialog;
import com.hzhjhh.view.manageView.menus.storage.InReturnGoodsDialog;

/**
 * 导航栏初始化
 * 
 * @author 12083
 *
 */
public class NavigationsInit {

	private static final Vector<String> SHOW_NAME = new Vector<>(Arrays.asList("库存编号", "商品编码", "商品名称", "库存数量"));
	
	private static StockMapper stockMapper;
	
	public static JTree init(MainFrame mainFrame) {
		JTree tree = new JTree(RootMgInit.init());
		
		tree.addTreeSelectionListener(treeSelect -> {
			Object obj1 = mainFrame.getjTree1().getLastSelectedPathComponent();
			String s = null;
			JTable jTable1 = mainFrame.getjTable1();
			JScrollPane jScrollPane2 = mainFrame.getjScrollPane2();
			if (obj1 != null) {
				s = obj1.toString();

				if (s.equals("销售统计")) {
//					Earning earn = new Earning();
//					earn.setVisible(true);
//					earn.setLocation(200, 0);
					SaleStatistics saleStatistics = new SaleStatistics();
				}

				if (s.equals("库存信息浏览")) {
					Vector vec1 = new Vector();
//					Vector vec2 = new Vector();
//					vec2.add("库存编号");
//					vec2.add("商品编码");
//					vec2.add("商品名称");
//					vec2.add("库存数量");
//					vec1 = Select_Means.Select_All_repertory();
					List<Stock> stockList = stockMapper.selectListByDTO(null);
					stockList.stream()
						.map(item -> {
							Vector rowV = new Vector<>();
							rowV.add(item.getIdStock());
							rowV.add(item.getMerchandiseCode());
							rowV.add(item.getMerchandiseName());
							rowV.add(item.getNumber());
							return rowV;
						})
						.forEach(item -> {
							vec1.add(item);
						});
					jTable1 = Mytable.maketable(vec1, SHOW_NAME); // 显示内容
					jScrollPane2.getViewport().add(jTable1); // 把表装入容器
				}
				if (s.equals("按库存商品编号查询")) {
					String str = JOptionPane.showInputDialog(mainFrame, "请输入商品编号", "提示", JOptionPane.INFORMATION_MESSAGE);
					if (str.length() == 0) {
						JOptionPane.showMessageDialog(mainFrame, "您没有输入商品编号", "提示", JOptionPane.INFORMATION_MESSAGE);
						return;
					}
					Vector vec1 = new Vector();
//					Vector vec2 = new Vector();
//					vec2.add("库存编号");
//					vec2.add("商品编码");
//					vec2.add("商品名称");
//					vec2.add("库存数量");
					StockDTO stockDTO = new StockDTO();
					stockDTO.setMerchandiseCode(str);
					List<Stock> stockList = stockMapper.selectListByDTO(stockDTO);
//					vec1 = Select_Means.Select_repertory(str);
					if (CollectionUtils.isEmpty(stockList)) {
						JOptionPane.showMessageDialog(mainFrame, "商品不存在", "提示", JOptionPane.INFORMATION_MESSAGE);
						return;
					}
					stockList.stream()
						.map(item -> {
							Vector rowV = new Vector<>();
							rowV.add(item.getIdStock());
							rowV.add(item.getMerchandiseCode());
							rowV.add(item.getMerchandiseName());
							rowV.add(item.getNumber());
							return rowV;
						})
						.forEach(item -> {
							vec1.add(item);
						});
					jTable1 = Mytable.maketable(vec1, SHOW_NAME); // 显示内容
					jScrollPane2.getViewport().add(jTable1); // 把表装入容器
				}

				if (s.equals("添加/修改用户")) {
					new Adminstrator();
//					Adminstrator admin_Enter = new Adminstrator();
//					admin_Enter.setVisible(true);
//					admin_Enter.setTitle("此功能需要管理员权限");
				}
				if (s.equals("个人账户查询")) {
//					Select_card seCard = new Select_card();
//					seCard.setVisible(true);
//					seCard.setTitle("个人账户查询");
					new AccountQueryDialog();
				}
				if (s.equals("入库/出库")) {
//					Put_Out_Depot put_out = new Put_Out_Depot();
//					put_out.setVisible(true);
//					put_out.setLocation(200, 100);
//					put_out.setTitle("进/出货表");
					new InOutTableDialog();
				}

				if (s.equals("进/退货")) {
//					JoinAndExceed join = new JoinAndExceed();
//					join.setVisible(true);
//					join.setTitle("进/退货表");
//					join.setLocation(200, 150);
					new InReturnGoodsDialog();
				}

				if (s.equals("新增商品")) {
//					AddMerchandise Merchandise = new AddMerchandise();
//					Merchandise.setVisible(true);
//					Merchandise.setTitle("新增商品表");
//					Merchandise.setLocation(60, 100);
					new AddGoodsDialog();
				}

				if (s.equals("电器类查询")) {
					QueryMerchandiseInfo.queryByCategory(mainFrame, CategoryEnums.DQ1004);
				}

				if (s.equals("礼品类查询")) {
					QueryMerchandiseInfo.queryByCategory(mainFrame, CategoryEnums.LP1006);
				}

				if (s.equals("日用类查询")) {
					QueryMerchandiseInfo.queryByCategory(mainFrame, CategoryEnums.RY1005);
				}

				if (s.equals("蔬果类查询")) {
					QueryMerchandiseInfo.queryByCategory(mainFrame, CategoryEnums.SG1003);
				}

				if (s.equals("食品类查询")) {
					QueryMerchandiseInfo.queryByCategory(mainFrame, CategoryEnums.SP1002);
				}

				if (s.equals("生鲜类查询")) {
					QueryMerchandiseInfo.queryByCategory(mainFrame, CategoryEnums.SX1001);
				}

				if (s.equals("按商品编号查询")) {
					QueryMerchandiseInfo.queryByDTO(mainFrame, QueryGoodsShowEnum.TREATY_CODE);
				}

				if (s.equals("按商品名称查询")) {
					QueryMerchandiseInfo.queryByDTO(mainFrame, QueryGoodsShowEnum.NAME);
				}

				if (s.equals("按商品条形码查询")) {
					QueryMerchandiseInfo.queryByDTO(mainFrame, QueryGoodsShowEnum.TREATY_CODE);
				}
			}
		});
		
		// 初始化mapper
		SqlSession sqlSession = DBUtils.getSqlSession(true);
		stockMapper = sqlSession.getMapper(StockMapper.class);
		
		return tree;
	}
}
